<template>
    <el-dialog :close-on-click-modal="false" ref="dialogform" :title="addEditName + title" :visible.sync="dialogVisible"
        width="50%" :before-close="handleClose">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="130px" class="demo-ruleForm">
            <el-form-item label="选择项目" v-show="addEditFlag == 'add'">
                <el-cascader class="w100p" :options="ownershipList"
                    :props="{ expandTrigger: 'hover', value: 'companyMark', children: 'child', label: 'ownershipName', checkStrictly: true, }"
                    clearable @change="ownership" placeholder="请选择项目" />
            </el-form-item>
            <el-form-item label="数据项选择" prop="iotDataItemId">
                <el-select class="w100p" :disabled="addEditFlag == 'edit'" v-model="ruleForm.iotDataItemId"
                    placeholder="请选择数据项" filterable clearable @change="depDataItem">
                    <el-option v-for="item in dataItemList" :key="item.dataItemId" :label="item.dataItemName"
                        :value="item.dataItemId" />
                </el-select>
            </el-form-item>
            <el-form-item label="展示数据项名称" prop="showDataItemName">
                <el-input class="w100p"  v-model="ruleForm.showDataItemName" placeholder="请输入数据项名称" clearable />
            </el-form-item>
            <el-form-item label="原始名称">
                <el-input class="w100p" disabled v-model="ruleForm.dataItemName" clearable placeholder="请选择数据项"></el-input>
            </el-form-item>
            <el-form-item label="计量类型" prop="meterFlag">
                <el-select class="w100p" v-model="ruleForm.meterFlag" placeholder="请选择计量类型" filterable clearable>
                    <el-option label="计量类型" :value="0" />
                    <el-option label="展示类型" :value="1" />
                </el-select>
            </el-form-item>
            <el-form-item label="是否展示" prop="showFlag">
                <el-select class="w100p" v-model="ruleForm.showFlag" placeholder="是否展示" filterable clearable>
                    <el-option label="展示" :value="true" />
                    <el-option label="不展示" :value="false" />
                </el-select>
            </el-form-item>
            <el-form-item label="选择单位" prop="unit">
                <el-select class="w100p" v-model="ruleForm.unit" filterable clearable placeholder="请选择父级区域"
                    @change="handelChangeUnit">
                    <el-option v-for="item, index in unitList" :key="index" :label="item.unitName + item.unit"
                        :value="item.unit">
                    </el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="cancel">取 消</el-button>
            <el-button type="primary" @click="submitForm">确 定</el-button>
        </span>
    </el-dialog>
</template>
<script>
import iotApplicationApi from '@/api/iotApplication'
import dataItemApi from '@/api/dataItem'
import unitApi from '@/api/unit'
export default {
    components: {},
    props: {
        addEditFlag: {
            type: String,
            default: () => {
                return 'add'
            }
        },
        data: {
            type: Object,
            default: () => {
                return {}
            }
        },
        title: {
            type: String,
            default: () => {
                return '操作表单'
            }
        },
        dialogVisible: {
            type: Boolean,
            default: () => {
                return false
            }
        },

        ownershipList: {
            type: Array,
            default: () => {
                return []
            }
        },
        energyList: {
            type: Array,
            default: () => {
                return []
            }
        },
    },
    data() {
        return {
            addEditName: '添加',
            addEditApi: '',
            ruleForm: {},
            dataItemList: [],
            unitList: [],
            // 0为报废，1为在使用，2为未使用
            stateList: [
                {
                    label: '报废',
                    value: 0
                },
                {
                    label: '在使用',
                    value: 1
                },
                {
                    label: '未使用',
                    value: 2
                }
            ],
            rules: {
                iotDataItemId: [
                    { required: true, message: '请选择数据项', trigger: 'change' }
                ],
                showDataItemName: [
                    { required: true, message: '请输入名称', trigger: 'change' }
                ],
                meterFlag: [
                    { required: true, message: '请选择计量类型', trigger: 'change' }
                ],
                showFlag: [
                    { required: true, message: '请选择展示类型', trigger: 'change' }
                ],
            }
        }
    },
    computed: {},
    watch: {
        dialogVisible(v) {
            v && this.initData();
        }
    },

    mounted() { },
    methods: {
        initData() {
            if (this.addEditFlag == 'add') {
                this.resetForm()
                this.addEditApi = dataItemApi.add
                this.addEditName = '添加';
                this.$set(this.ruleForm, 'showFlag', true);
            } else if (this.addEditFlag == 'edit') {
                this.ruleForm = { ...this.data }
                this.addEditApi = dataItemApi.edit
                this.addEditName = '编辑';
                this.queryRtuByCompanyMark(sessionStorage.organizationNo)
            }
            this.queryByCondition();
        },

        async queryByCondition() {
            let rut = await unitApi.queryByCondition({});
            this.unitList = rut.data || []
        },
        ownership(e) {
            if (e) {
                let mk = e.slice(-1)
                this.queryRtuByCompanyMark(mk)
            }
        },
        depDataItem(id) {
            let rut = this.dataItemList.find(e => e.dataItemId == id);
            this.$set(this.ruleForm, 'dataItemName', rut ? rut.dataItemName : '')
            this.$set(this.ruleForm, 'showDataItemName', rut ? rut.dataItemName : '')
        },
        handelChangeUnit(unit) {
            let rut = this.unitList.find(e => e.unit == unit);
            this.$set(this.ruleForm, 'unitName', rut ? rut.unitName : '')
        },
        queryRtuByCompanyMark(mk) {
            iotApplicationApi.queryDataItemByCompanyMark(mk).then(res => {
                let data = res.data;
                this.dataItemList = data;
            });
        },
        //提交表单
        submitForm() {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    this.addEditApi({ ...this.ruleForm }).then(res => {
                        let msg = res.code == 1 ? '操作成功' : '操作失败'
                        this.$message[res.code == 1 ? 'success' : 'warning'](res.msg || msg)
                        res.code == 1 && this.cancel();
                    }).catch(err => {
                        console.log(err)
                    })
                } else {
                    return false;
                }
            })
        },
        //重置
        resetForm() {
            this.ruleForm = {};
            this.$refs.ruleForm && this.$refs.ruleForm.resetFields()
        },
        //取消关闭
        cancel() {
            this.resetForm()
            this.$emit("closeDialog")
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => { this.resetForm(); this.cancel() })
                .catch(_ => { });
        }
    }
};
</script>
<style lang="scss" scoped></style>